package com.wuchengdong.web.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuchengdong.web.extend.utils.R;
import com.wuchengdong.web.pojo.Employee;
import com.wuchengdong.web.pojo.Service;
import com.wuchengdong.web.pojo.ServiceAndEmployee;
import com.wuchengdong.web.service.EmployeeService;
import com.wuchengdong.web.service.ServiceAndEmployeeService;
import com.wuchengdong.web.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/serviceAndEmployee")
public class ServiceAndEmployeeController {
    @Autowired
    ServiceService serviceService;
    @Autowired
    EmployeeService employeeService;
    @Autowired
    ServiceAndEmployeeService serviceAndEmployeeService;

    @GetMapping("/getByService")
    public R<List<ServiceAndEmployee>> getEmpByService(Integer serviceId) {
        return R.success(serviceAndEmployeeService.list(
                new LambdaQueryWrapper<ServiceAndEmployee>()
                        .eq(ServiceAndEmployee::getServiceId, serviceId)
        ));
    }

    @GetMapping("/getByServiceReverse")
    public R<List<Employee>> getEmpByServiceReverse(Integer serviceId) {
        List<ServiceAndEmployee> employees = serviceAndEmployeeService.list(
                new LambdaQueryWrapper<ServiceAndEmployee>()
                        .eq(ServiceAndEmployee::getServiceId, serviceId)
        );
        List<Integer> empIdList = employees.stream().map(ServiceAndEmployee::getEmployeeId).toList();
        return R.success(employeeService.list(
                new LambdaQueryWrapper<Employee>()
                        .notIn(ObjectUtil.isNotEmpty(empIdList),Employee::getId, empIdList)
                        .orderByAsc(Employee::getId)
        ));
    }

    @GetMapping("/getByEmployee")
    public R<List<ServiceAndEmployee>> getByEmployee(Integer employeeId) {
        return R.success(serviceAndEmployeeService.list(
                new LambdaQueryWrapper<ServiceAndEmployee>()
                        .eq(ServiceAndEmployee::getEmployeeId, employeeId)
        ));
    }

    @GetMapping("/getByEmployeeReverse")
    public R<List<Service>> getByEmployeeReverse(Integer employeeId) {
        List<ServiceAndEmployee> services = serviceAndEmployeeService.list(
                new LambdaQueryWrapper<ServiceAndEmployee>()
                        .eq(ServiceAndEmployee::getEmployeeId, employeeId)
        );
        List<Integer> serviceIdList = services.stream().map(ServiceAndEmployee::getServiceId).toList();
        return R.success(serviceService.list(
                new LambdaQueryWrapper<Service>()
                        .notIn(Service::getId, serviceIdList)
                        .orderByAsc(Service::getId)
        ));
    }

    @PostMapping
    public R<String> save(@RequestBody ServiceAndEmployee se) {
        if (ObjectUtil.isEmpty(se.getEmployeeName())) {
            se.setEmployeeName(employeeService.getById(se.getEmployeeId()).getName());
        }
        if (ObjectUtil.isEmpty(se.getServiceName())) {
            se.setServiceName(serviceService.getById(se.getServiceId()).getName());
        }
        return serviceAndEmployeeService.save(se) ? R.success("保存成功") : R.error("保存失败");
    }

    @DeleteMapping
    public R<String> delete(@RequestBody ServiceAndEmployee se) {
        // 经过调查 如果两个eq都为空，不会全部删除 会抛出异常
        return serviceAndEmployeeService.remove(
                new LambdaQueryWrapper<ServiceAndEmployee>()
                        .eq(ObjectUtil.isNotEmpty(se.getServiceId()), ServiceAndEmployee::getServiceId, se.getServiceId())
                        .eq(ObjectUtil.isNotEmpty(se.getEmployeeId()), ServiceAndEmployee::getEmployeeId, se.getEmployeeId())
        ) ? R.success("删除成功") : R.error("删除失败");
    }
}
